# 编写正则爬虫   https://tv.2345.com/top/

import requests
import re

cookies = {
    'YSRF': 'own',
    'uUiD': '223061734673203068927',
    'Hm_lvt_9bb494db4665e40147cd57b89d6628e2': '1734673204',
    'Hm_lpvt_9bb494db4665e40147cd57b89d6628e2': '1734673204',
    'HMACCOUNT': 'B2C21EEE3F1F23B1',
}

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    # 'Cookie': 'YSRF=own; uUiD=223061734673203068927; Hm_lvt_9bb494db4665e40147cd57b89d6628e2=1734673204; Hm_lpvt_9bb494db4665e40147cd57b89d6628e2=1734673204; HMACCOUNT=B2C21EEE3F1F23B1',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

if __name__ == '__main__':
    response = requests.get('https://tv.2345.com/top/', cookies=cookies, headers=headers)
    data = re.findall(r'target="_blank" title="(.*?)" {2}data-ajax25module="排行榜"', response.text, re.S)
    with open('movie_names.txt','a',encoding='utf8') as f:
        for i in data:
            f.write(i+'\n')
